import {
  Inject,
  Controller,
  Get,
  Query,
  Post,
  Body,
} from "@midwayjs/core/dist";
import { Context } from "@midwayjs/koa/dist";
import { UserService } from "../service/user.service";
import { Resultes } from "../interface";

@Controller("/user")
export class UserController {
  @Inject()
  ctx: Context;

  @Inject()
  userService: UserService;

  @Get("/list")
  async getList(
    @Query("pageSize") pageSize = 2,
    @Query("pageNum") pageNum = 1,
    @Query("username") username: string
  ) {
    const users = await this.userService.getList(pageSize, pageNum, username);
    return Resultes.success(users);
  }

  @Get("/getUser")
  async getUser(@Query("id") id) {
    const user = await this.userService.getUser(id);
    return Resultes.success(user);
  }

  @Post("/register")
  async register(@Body() userEntity) {
    // const request = this.ctx.request;
    // console.error('request--', request);

    const user = await this.userService.register(userEntity);
    return Resultes.success(user);
  }

  @Post("/login")
  async login(@Body() userEntity) {
    const params = {
      username: userEntity.username,
      password: userEntity.password,
    };
    const user = await this.userService.login(params);
    return Resultes.success(user);
  }
}
